<?php

class DeliverController extends Zend_Controller_Action
{

    protected $_form = null;

    protected $_namespace = 'DeliverController';

    protected $_session = null;

    public function init()
    {
        /* Initialize action controller here */
    	
    }

    public function indexAction()
    {
        // action body
    	// Either re-display the current page, or grab the "next" (first)
    	// sub form
    	$form = $this->getCurrentSubForm();
    	if (!$form) {
    		$form = $this->getNextSubForm();
    	}
    	$this->view->form = $this->getForm()->prepareSubForm($form);
    }

    public function processAction()
    {
        // action body
    	if (!$form = $this->getCurrentSubForm()) {
    		//not exists return to default page
    		$this->view->info = $this->getSessionNamespace();
    		
    		return $this->_forward('index');
    	}
    	//$this->view->msg ="subFormName=".$form->getName();
    	if (!$this->subFormIsValid($form, $this->getRequest()->getPost())) {
    		$this->view->form = $this->getForm()->prepareSubForm($form);
    		$this->view->info = $this->getSessionNamespace();
    		//$this->view->msg ="break1  subFormName=".$this->view->form->getName();
    		return $this->render('index');
    	}
    	
    	if (!$this->formIsValid()) {
    		$form = $this->getNextSubForm();
    		if($form){
    			$this->view->form = $this->getForm()->prepareSubForm($form);
    			$this->view->info = $this->getSessionNamespace();
    			//$this->view->msg ="break2";
    			return $this->render('index');
    		}
    		
    	}
    	$this->view->msg ="break3";
    	// Valid form!
    	// Render information in a verification page
    	$this->view->info = $this->getSessionNamespace();
    	$this->_forward('validation');
    	//$this->getSessionNamespace()->unsetAll();
    	
    }
    
    public function validationAction()
    {
    	// action body
    }

    public function getForm()
    {
    	if (!$this->_form) {
    		$this->_form = new Application_Form_Deliver();
    	}
    	return $this->_form;
    }

    /**
     * Get the session namespace we're using
     *
     * @return Zend_Session_Namespace
     *
     */
    public function getSessionNamespace()
    {
    	if (null === $this->_session) {
    		require_once 'Zend/Session/Namespace.php';
    		$this->_session = new Zend_Session_Namespace($this->_namespace);
    	}
    
    	return $this->_session;
    }

    /**
     * Get a list of forms already stored in the session
     *
     * @return array
     *
     */
    public function getStoredForms()
    {
    	$stored = array();
    	foreach ($this->getSessionNamespace() as $key => $value) {
    		$stored[] = $key;
    	}
    
    	return $stored;
    }

    /**
     * Get list of all subforms available
     *
     * @return array
     *
     */
    public function getPotentialForms()
    {
    	return array_keys($this->getForm()->getSubForms());
    }

    /**
     * What sub form was submitted?
     *
     * @return false|Zend_Form_SubForm
     *
     */
public function getCurrentSubForm()
    {
        $request = $this->getRequest();
        if (!$request->isPost()) {
        	$this->getSessionNamespace()->unsetAll();
            foreach ($this->getPotentialForms() as $name) {
                return $this->getForm()->getSubForm($name);
            }
        }

        foreach ($this->getPotentialForms() as $name) {
            if ($data = $request->getPost($name, false)) {
                if (is_array($data)) {
                    return $this->getForm()->getSubForm($name);
                    break;
                }
            }
        }

        return false;
    }
    

    /**
     * Get the next sub form to display
     *
     * @return Zend_Form_SubForm|false
     *
     */
    public function getNextSubForm()
    {
    	$storedForms    = $this->getStoredForms();
    	$potentialForms = $this->getPotentialForms();
    
    	foreach ($potentialForms as $name) {
    		if (!in_array($name, $storedForms)) {
    			return $this->getForm()->getSubForm($name);
    		}
    	}
    
    	return false;
    }

    /**
     * Is the sub form valid?
     *
     * @param  Zend_Form_SubForm $subForm
     * @param  array $data
     * @return bool
     *
     */
    public function subFormIsValid(Zend_Form_SubForm $subForm, array $data)
    {
    	$name = $subForm->getName();
    	if ($subForm->isValid($data)) {
    		$this->getSessionNamespace()->$name = $subForm->getValues();
    		return true;
    	}
    
    	return false;
    }

    /**
     * Is the full form valid?
     *
     * @return bool
     *
     */
    public function formIsValid()
    {
    	$data = array();
    	foreach ($this->getSessionNamespace() as $key => $info) {
    		$data[$key] = $info;
    	}
    
    	return $this->getForm()->isValid($data);
    }




}





